package com.bulletelc.apollopre.module.display.product.details.comment;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;
import com.bulletelc.apollopre.widget.ColorFilterImageView;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/15
 * desc  : 商品评论图片的adapter
 */

public class ImagesAdapter extends BaseAdapter implements View.OnClickListener {
    private List<String> list;
    private Context context;
    private LayoutInflater inflater;

    public ImagesAdapter(Context context, List<String> list) {
        setImgPaths(list);
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setImgPaths(List<String> imgPaths) {
        if (imgPaths == null) {
            imgPaths = new ArrayList<>();
        }
        this.list = imgPaths;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.comment_img_item, parent, false);
            holder = new ViewHolder();
            holder.ivImg = convertView.findViewById(R.id.ivImg);
            holder.ivImg.setOnClickListener(this);
            convertView.setTag(R.id.tag_comment_show_gv,holder);
            AutoUtils.autoSize(convertView);//适配屏幕
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_comment_show_gv);
        }

        holder.ivImg.setTag(R.id.tag_comment_show_gv_img, position);

        ImageLoaderManager.getInstance().loadImage(
                ImageLoaderManager.getCommentImgeOptions(holder.ivImg, list.get(position)));
        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivImg:
                int position = (int) v.getTag(R.id.tag_comment_show_gv_img);
                Intent intent = new Intent(context, ImagePagerActivity.class);
                intent.putExtra(IntentCode.EXTRA_KEY_IMAGE_CURRENT_POSITION, position);
                intent.putStringArrayListExtra(IntentCode.EXTRA_KEY_IMAGES_PATH, (ArrayList<String>) list);
                context.startActivity(intent);
                break;
        }

    }


    class ViewHolder {
        ColorFilterImageView ivImg;
    }

}
